<template>
  <div class="progress-features-page">
    <div class="breadcrumb">
      <Breadcrumb :data="breadcrumbData"></Breadcrumb>
    </div>
    <div class="search-container">
      <div class="nav">
        <el-button :type="fileType === '1' ? 'primary' : ''" @click="handleChange('1')">航拍照片</el-button>
        <el-button :type="fileType === '2' ? 'primary' : ''" @click="handleChange('2')">720云</el-button>
        <el-button :type="fileType === '3' ? 'primary' : ''" @click="handleChange('3')">日常照片</el-button>
        <el-button :type="fileType === '4' ? 'primary' : ''" @click="handleChange('4')">视 频</el-button>
      </div>
      <div class="btn">
        <el-button v-if="hasTabUpload" type="primary" @click="handleUpload">上 传</el-button>
      </div>
    </div>
    <main>
      <div class="list-container" v-loading="listLoading">
        <div class="title">
          <h3>文件列表</h3>
          <i v-if="hasTabAdd" class="el-icon-plus" @click="handleAdd"></i>
        </div>
        <ul>
          <li
            :class="['list-item', item.id === activeId ? 'active' : '']"
            v-for="item in dirList"
            :key="item.id"
            @click="handleSelect(item)">
            <span class="text">{{ item.dirName }}</span>
            <div class="btns">
              <i v-if="hasTableEdit" class="el-icon-edit" @click.stop="handleEdit(item)"></i>
              <i v-if="hasTableDel" class="el-icon-delete" @click.stop="handleDelete(item)"></i>
              <i class="el-icon-sort"></i>
            </div>
          </li>
        </ul>
      </div>
      <div class="detail-container" v-loading="fileLoading">
        <div v-if="!fileList.length" class="empty-content">
          <i class="iconfont iconwushuju"></i>
          <p>暂无数据</p>
        </div>
        <div class="detail" v-for="item in fileList" :key="item.id">
          <span class="time">{{ item.modifyTime }}</span>
          <template v-if="/\.jpg|\.jpeg|\.png$/.test(item.fileUrl)">
            <el-image
              lazy
              :src="$checkUrl(item.fileUrl)"
              :preview-src-list="previewList">
            </el-image>
          </template>
          <template v-else-if="fileType === '2'">
            <el-image
              lazy
              :src="cloudImg"
              @click="handleLink(item.fileUrl)">
            </el-image>
          </template>
          <template v-else>
            <video
              style="width:100%;height:100%;"
              controls="controls"
              :src="$checkUrl(item.fileUrl)">
            </video>
          </template>
          <el-tooltip v-if="item.remark" class="tooltip" effect="dark" placement="top">
            <div slot="content">备注：<br/><br/>{{ item.remark }}</div>
            <p class="file-name">{{ item.fileName }}</p>
          </el-tooltip>
          <p v-else class="file-name">{{ item.fileName }}</p>
        </div>
      </div>
    </main>
    <el-dialog
      class="upload-features-dir"
      title="文件夹设置"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      :visible="visible"
      :width="$setRem(500)"
      :before-close="handleCancel">
      <div class="form-container">
        <el-form
          :model="ruleForm"
          :rules="rules"
          :label-width="$setRem(140)"
          ref="featuresDirForm"
          class="rule-form">
          <el-form-item label="文件夹名称" prop="dirName" required>
            <el-input placeholder="请输入文件夹名称" v-model="ruleForm.dirName" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleCancel">取 消</el-button>
        <el-button
          class="success"
          type="primary"
          :loading="loading"
          @click="handleSubmit">
          确 认
        </el-button>
      </div>
    </el-dialog>
    <upload-features-file
      :visible="fileVisible"
      :dirList="dirList"
      :loading="btnLoading"
      :fileType="fileType"
      :dirId="activeId"
      :accept="accept"
      @submit="handleSave"
      @cancel="fileVisible = false">
    </upload-features-file>
  </div>
</template>

<script>
import Breadcrumb from '@/components/public/Breadcrumb';
import uploadFeaturesFile from '@/components/project/progress/uploadFeaturesFile';
import cloudImg from '../../../assets/images/default-720.jpeg';

export default {
  data() {
    return {
      breadcrumbData: [
        { name: '进度管理' },
        { name: '进度面貌' }
      ],
      cloudImg,
      activeId: '',
      listLoading: false,
      type: 'add',
      currentData: {},
      fileType: '1',
      fileList: [],
      dirList: [],
      fileLoading: false,
      fileVisible: false,
      visible: false,
      loading: false,
      btnLoading: false,
      ruleForm: {
        dirName: ''
      },
      rules: {
        dirName: [
          { required: true, message: '请输入文件夹名称' }
        ]
      }
    };
  },
  computed: {
    projectId() {
      return this.$store.state.currentProjectId;
    },
    // 当前选中的文件夹
    activeData() {
      return this.dirList.find(v => v.id === this.activeId) || {};
    },
    accept() {
      switch (this.fileType) {
        case '1':
        case '3': return '.jpg,.png,.jpeg';
        case '4': return '.mp4,.WebM,.MPEG,.MPG,.DAT,.Ogg';
        default: return '';
      }
    },
    // 图片预览
    previewList() {
      return this.fileList
        .filter(item => item.fileUrl)
        .map(v => this.$checkUrl(v.fileUrl));
    }
  },
  watch: {
    activeId(val) {
      if (val) {
        this.getFileList();
      }
    }
  },
  created() {
    this.getDirList(true);
  },
  components: {
    Breadcrumb,
    uploadFeaturesFile
  },
  methods: {
    // 切换tab
    handleChange(type) {
      this.fileType = type;
      this.activeId = '';
      this.fileList = [];
      this.getDirList(true);
    },
    // 上传
    handleUpload() {
      this.fileVisible = true;
    },
    handleSelect(item) {
      this.activeId = item.id;
    },
    handleLink(url) {
      window.open(url);
    },
    // 新增文件夹
    handleAdd() {
      this.currentData = {};
      this.type = 'add';
      this.ruleForm = {
        dirName: ''
      };
      this.visible = true;
    },
    // 编辑文件夹
    handleEdit(item) {
      this.currentData = item;
      this.ruleForm = {
        dirName: item.dirName
      };
      this.type = 'edit';
      this.visible = true;
    },
    // 删除文件夹
    handleDelete(item) {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        const { success } = await this.$Model.deleteDirList({
          ids: item.id
        });
        if (success) {
          this.$message.success('删除成功');
          this.getDirList();
        }
      });
    },
    // 取消文件夹设置
    handleCancel() {
      this.visible = false;
      this.loading = false;
    },
    // 保存文件夹设置
    handleSubmit() {
      this.$refs.featuresDirForm.validate(async valid => {
        if (valid) {
          const params = {
            ...this.ruleForm,
            projectId: this.projectId,
            fileType: this.fileType,
          };
          this.loading = true;
          const { success } = this.type === 'add'
            ? await this.$Model.addDirList(params)
            : await this.$Model.updateDirList({
              ...params,
              id: this.currentData.id
            });
          this.loading = false;
          if (success) {
            this.getDirList();
            this.$message.success('保存成功');
            this.visible = false;
          }
        }
      });
    },
    async handleSave(data) {
      if (!data) return;
      const { success } = await this.$Model.addScheduleFile({
        ...data,
        projectId: this.projectId,
        fileType: this.fileType,
      });
      if (success) {
        this.getFileList();
        this.$message.success('保存成功');
        this.fileVisible = false;
      }
    },
    // 获取文件信息
    async getFileList(hasInit = false) {
      this.fileLoading = true;
      const { success, data: { data }} = await this.$Model.getScheduleFile({
        pageFlag: false,
        dirId: this.activeId,
        fileType: this.fileType,
        projectId: this.projectId
      });
      this.fileLoading = false;
      if (success && data) {
        this.fileList = data.list || [];
      }
    },
    // 获取文件夹信息
    async getDirList(hasInit = false) {
      this.listLoading = true;
      const { success, data: { data }} = await this.$Model.getDirList({
        pageFlag: false,
        fileType: this.fileType,
        projectId: this.projectId
      });
      this.listLoading = false;
      if (success && data) {
        this.dirList = data.list || [];
        if (hasInit && this.dirList.length) {
          this.activeId = this.dirList[0].id;
        }
      }
    }
  }
};
</script>

<style scoped lang="scss">
  @import "../../../assets/styles/main";
  @import "../../../assets/styles/dialog";

  .progress-features-page {
    width: 100%;
    height: 100%;
    background: $baseBg;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .search-container {
      width: 100%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      .nav .el-button {
        width: 120px;
        margin-right: 20px;
      }
    }
    main {
      height: calc(100% - 100px);
      width: 100%;
      padding: 20px;
      display: flex;
      justify-content: space-between;
      .list-container {
        width: 350px;
        height: 100%;
        padding: 20px;
        border-radius: 10px;
        background-color: $mainBg;
        .title {
          width: 100%;
          height: 30px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .el-icon-plus {
            width: 25px;
            height: 25px;
            text-align: center;
            line-height: 25px;
            background-color: rgba(160, 164, 179, 0.4);
            border-radius: 5px;
            cursor: pointer;
            &:hover {
              color: $lightBtnBg;
            }
          }
        }
        ul {
          width: 100%;
          height: calc(100% - 30px);
          overflow: auto;
          padding: 20px 0;
          .list-item {
            width: 100%;
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: $lightBtnBg;
            border-radius: 5px;
            padding: 0 10px;
            cursor: pointer;
            transition: background-color .3s;
            margin-bottom: 15px;
            &.active {
              background-color: $mainBtnBg;
            }
            &:hover {
              background-color: $mainBtnBg;
            }
            .btns {
              width: 70px;
              display: flex;
              justify-content: space-between;
              align-items: center;
              i {
                &:hover {
                  color: $lightBtnBg;
                }
              }
            }
            .text {
              width: calc(100% - 90px);
              display: inline-block;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }
        }
      }
      .detail-container {
        width: calc(100% - 350px);
        height: 100%;
        overflow: auto;
        padding: 20px 0 20px 20px;
        .empty-content {
          width: 100%;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          font-size: 22px;
          color: $mainColor;
          i {
            font-size: 100px;
            margin-bottom: 20px;
          }
        }
        .detail {
          width: calc(25% - 20px);
          height: calc(33.3% - 40px);
          position: relative;
          margin: 20px 20px 20px 0;
          float: left;
          .time {
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 16px;
            color: #000;
            z-index: 1;
            font-family: font-lcd;
          }
          .file-name {
            margin-top: 10px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            cursor: pointer;
            font-size: 18px;
            &:hover {
              color: $mainColor;
            }
          }
          .el-image {
            width: 100%;
            height: calc(100% - 20px);
            padding: 10px;
            background: linear-gradient(0deg, rgb(246, 250, 255) 10%, rgb(214, 224, 235) 70%);
            border-radius: 10px;
            cursor: pointer;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .iframe-cloud {
            width: 100%;
            height: calc(100% - 20px);
            padding: 10px;
            background: linear-gradient(0deg, rgb(246, 250, 255) 10%, rgb(214, 224, 235) 70%);
          }
        }
      }
    }
  }

  .upload-features-dir {
    .form-container {
      /deep/ .el-form-item__label {
        color: #fff;
        .el-upload-list__item-name {
          color: #fff;
        }
      }
      /deep/ .el-input {
        width: 300px;
      }
    }
  }
</style>
